<template>
	<view class="container">
		<view class="form">
			<view class="form-item">
				<text class="label">反馈类型</text>
				<picker @change="onTypeChange" :value="typeIndex" :range="feedbackTypes">
					<view class="picker">
						<text :class="{ placeholder: !form.type }">{{form.type || '请选择反馈类型'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</picker>
			</view>
			
			<view class="form-item">
				<text class="label">问题描述</text>
				<textarea 
					v-model="form.content" 
					placeholder="请详细描述您遇到的问题或建议"
					:maxlength="500"
					class="content"
				></textarea>
				<text class="word-count">{{form.content.length}}/500</text>
			</view>
			
			<view class="form-item">
				<text class="label">上传图片</text>
				<view class="upload-area">
					<view class="image-list">
						<view 
							class="image-item" 
							v-for="(image, index) in form.images" 
							:key="index"
							@tap="previewImage(index)"
						>
							<image :src="image" mode="aspectFill"></image>
							<view class="delete-btn" @tap.stop="deleteImage(index)">
								<uni-icons type="close" size="12" color="#fff"></uni-icons>
							</view>
						</view>
						<view class="upload-btn" @tap="chooseImage" v-if="form.images.length < 3">
							<uni-icons type="camera" size="24" color="#999"></uni-icons>
							<text>{{form.images.length}}/3</text>
						</view>
					</view>
					<text class="tip">最多上传3张图片</text>
				</view>
			</view>
			
			<view class="form-item">
				<text class="label">联系方式</text>
				<input type="text" v-model="form.contact" placeholder="请输入您的手机号或邮箱（选填）" />
			</view>
		</view>
		
		<view class="submit-btn" :class="{ disabled: !canSubmit }" @tap="submitFeedback">
			提交反馈
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			feedbackTypes: ['功能异常', '体验问题', '产品建议', '其他'],
			typeIndex: 0,
			form: {
				type: '',
				content: '',
				images: [],
				contact: ''
			}
		}
	},
	computed: {
		canSubmit() {
			return this.form.type && this.form.content.trim()
		}
	},
	methods: {
		onTypeChange(e) {
			this.typeIndex = e.detail.value
			this.form.type = this.feedbackTypes[this.typeIndex]
		},
		chooseImage() {
			uni.chooseImage({
				count: 3 - this.form.images.length,
				sizeType: ['compressed'],
				sourceType: ['album', 'camera'],
				success: (res) => {
					// TODO: 上传图片到服务器
					this.form.images = [...this.form.images, ...res.tempFilePaths]
				}
			})
		},
		previewImage(index) {
			uni.previewImage({
				urls: this.form.images,
				current: this.form.images[index]
			})
		},
		deleteImage(index) {
			this.form.images.splice(index, 1)
		},
		submitFeedback() {
			if (!this.canSubmit) return
			
			if (this.form.contact && !this.validateContact()) {
				uni.showToast({
					title: '请输入正确的联系方式',
					icon: 'none'
				})
				return
			}
			
			uni.showLoading({
				title: '提交中...'
			})
			
			// TODO: 调用后端API提交反馈
			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '提交成功',
					icon: 'success'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}, 1000)
		},
		validateContact() {
			const phoneReg = /^1\d{10}$/
			const emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/
			return !this.form.contact || phoneReg.test(this.form.contact) || emailReg.test(this.form.contact)
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	padding-bottom: 120rpx;
}

.form {
	background-color: #fff;
	padding: 0 30rpx;
	margin-top: 20rpx;
	
	.form-item {
		padding: 30rpx 0;
		border-bottom: 1rpx solid $border-color;
		
		&:last-child {
			border-bottom: none;
		}
		
		.label {
			font-size: 28rpx;
			color: $text-color;
			margin-bottom: 20rpx;
			display: block;
		}
		
		.picker {
			display: flex;
			justify-content: space-between;
			align-items: center;
			
			text {
				font-size: 28rpx;
				color: $text-color;
				
				&.placeholder {
					color: $text-color-grey;
				}
			}
		}
		
		.content {
			width: 100%;
			height: 240rpx;
			font-size: 28rpx;
			line-height: 1.5;
		}
		
		.word-count {
			font-size: 24rpx;
			color: $text-color-grey;
			text-align: right;
			margin-top: 10rpx;
			display: block;
		}
		
		.upload-area {
			.image-list {
				display: flex;
				flex-wrap: wrap;
				margin: 0 -10rpx;
				
				.image-item, .upload-btn {
					width: 180rpx;
					height: 180rpx;
					margin: 10rpx;
					border-radius: 8rpx;
				}
				
				.image-item {
					position: relative;
					
					image {
						width: 100%;
						height: 100%;
						border-radius: 8rpx;
					}
					
					.delete-btn {
						position: absolute;
						top: -16rpx;
						right: -16rpx;
						width: 32rpx;
						height: 32rpx;
						background-color: rgba(0, 0, 0, 0.5);
						border-radius: 16rpx;
						display: flex;
						align-items: center;
						justify-content: center;
					}
				}
				
				.upload-btn {
					background-color: #f8f8f8;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					
					text {
						font-size: 24rpx;
						color: $text-color-grey;
						margin-top: 10rpx;
					}
				}
			}
			
			.tip {
				font-size: 24rpx;
				color: $text-color-grey;
				margin-top: 20rpx;
				display: block;
			}
		}
		
		input {
			font-size: 28rpx;
		}
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: #2979ff;
	color: #fff;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 32rpx;
	font-weight: bold;
	
	&.disabled {
		opacity: 0.5;
	}
}
</style> 